home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 284_01 / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-11  |  2.9 KB  |  156 lines

  1. /* util.c - general utility routines for monitor */
  2.  
  3. #include <stdio.h>
  4. #include "compile.h"
  5. #include "config.h"
  6. #include "defs.h"
  7.  
  8. #ifndef isdigit
  9. #define isdigit(c)    ( (c) >= '0' && (c) <= '9' )
  10. #endif
  11.  
  12. BYTE sum;
  13.  
  14. static char hexch [] = {
  15.     '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
  16. };
  17.  
  18. /* general utilities */
  19.  
  20. fputx ( b, fp )
  21.     BYTE b;
  22.     register FILE * fp;
  23. {
  24.     putc( hexch[ b >> 4      ], fp );
  25.     putc( hexch[ b & 0x0F ], fp );
  26. }
  27.  
  28. fputxx ( w, fp )
  29.     WORD w;
  30.     register FILE * fp;
  31. {
  32.     putc( hexch[ Hi( w ) >> 4    ], fp );
  33.     putc( hexch[ Hi( w ) & 0x0F ], fp );
  34.     putc( hexch[ Lo( w ) >> 4    ], fp );
  35.     putc( hexch[ Lo( w ) & 0x0F ], fp );
  36. }
  37.  
  38. /* Intel HEX format loader ( for IPL and warm boot ) */
  39.  
  40. static BYTE hex [ 257 ] = {
  41.     0,
  42.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  43.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  44.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  45.     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,0,0,0,0,0,
  46.     0, 10, 11, 12, 13, 14, 15
  47. };
  48.  
  49. gethex ( fp )
  50.     register FILE * fp;
  51. {
  52.     BYTE x;
  53.  
  54.     x =     hex[ getc( fp ) + 1 ] << 4;
  55.     x |= hex[ getc( fp ) + 1 ];
  56.     sum += x;
  57.     return( x );
  58. }
  59.  
  60. loader ( fid )
  61.     char * fid;
  62. {
  63.     register BYTE * p;
  64.     register FILE * fp;
  65.     register BYTE x;
  66.     int c, n;
  67.     WORD ad;
  68.  
  69.     fp = fopen( fid, "r" );
  70.     if ( fp == ( FILE * )NULL ) return( 1 );
  71.  
  72.     for (;;) {
  73.     while ( ( c = getc( fp ) ) != EOF && c != ':' ) ;
  74.     if ( c == EOF ) break;
  75.     sum = 0;
  76.     if ( ( n = gethex( fp ) ) == 0 ) break;
  77.     ad = gethex( fp ) << 8,
  78.     ad |= gethex( fp );
  79.     if ( gethex( fp ) != 0 ) {
  80.         fclose( fp );
  81.         return( 2 );
  82.     }
  83.     p = &Mem[ ad ];
  84.     while ( --n >= 0 ) {
  85.         *p++ = gethex( fp );
  86.     }
  87.     gethex( fp );
  88.     if ( sum ) {
  89.         fclose( fp );
  90.         return( 2 );
  91.     }
  92.     }
  93.  
  94.     if ( feof( fp ) ) {
  95.     fclose( fp );
  96.     return( 2 );
  97.     } else {
  98.     for ( n = 0; n < 8; n++ ) {
  99.         if ( getc( fp ) != '0' ) {
  100.         fclose( fp );
  101.         return( 2 );
  102.         }
  103.     }
  104.     fclose( fp );
  105.     return( 0 );
  106.     }
  107. }
  108.  
  109. /* machine aborted - exit with/without core dump */
  110.  
  111. puthex ( b, fp )
  112.     int b;
  113.     FILE * fp;
  114. {
  115.     fputx( b, fp );
  116.     sum += b;
  117. }
  118.  
  119. coredump ( core )
  120.     char * core;
  121. {
  122.     register BYTE * p;
  123.     register FILE * fp;
  124.     register WORD i;
  125.  
  126.     fp = fopen( core, "w" );
  127.     if ( fp == ( FILE * )NULL ) {
  128.     fputs( "Cannot write to: ", stderr );
  129.     fputs( core, stderr );
  130.     fputs( "\n", stderr );
  131.     return( 1 );
  132.     }
  133.  
  134.     for ( i = 0; i < MEMSIZE ; i += 32 ) {
  135.     sum = 0;
  136.     putc( ':', fp );
  137.     puthex( 0x20, fp );
  138.     puthex( i >> 8,      fp );
  139.     puthex( i & 0xFF, fp );
  140.     puthex( 0x00, fp );
  141.     for ( p = &Mem[ i ]; p < &Mem[ i + 32 ]; p++ ) {
  142.         puthex( *p, fp );
  143.     }
  144.     puthex( -sum & 0xFF, fp );
  145.     putc( '\n', fp );
  146.     }
  147.  
  148.     fputs( ":0000000000\n", fp );
  149.     fclose( fp );
  150.  
  151.     fputs( core, stderr );
  152.     fputs( " dumped\n", stderr );
  153.  
  154.     return( 0 );
  155. }
  156.